package com.site.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.junit.jupiter.api.Test;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
import java.util.Random;

@SuppressWarnings("all")
public class JDBCUtils {

    private static DataSource dataSource;
    static {
        try {
            InputStream inputStream = JDBCUtils.class.getResourceAsStream("/database.properties");
            Properties properties = new Properties();
            properties.load(inputStream);
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static DataSource getDataSource() {
        return dataSource;
    }

    public static Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }

    /*事务的增加删除修改*/
    public static int executeUpdate(Connection conn,String sql,Object[] params){
        int num = 0;
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement(sql);
            //参数
            if(params != null){
                for(int i=0;i<params.length;i++){
                    pstmt.setObject(i+1,params[i]);
                }
            }
            num = pstmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    /*没有事务的增加删除修改*/
    public static int executeUpdate(String sql,Object[] params){
        int num = 0;
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = getConnection();
            pstmt = conn.prepareStatement(sql);
            //参数
            if(params != null){
                for(int i=0;i<params.length;i++){
                    pstmt.setObject(i+1,params[i]);
                }
            }
            num = pstmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            close(pstmt,conn);
        }
        return num;
    }

    /*封装的查询*/
    public static ResultSet executeQuery(Connection conn,String sql,Object... params){
        ResultSet rs = null;
        PreparedStatement pstmt = null;
        try{
            pstmt = conn.prepareStatement(sql);
            if(params != null){
                for(int i=0;i<params.length;i++){
                    pstmt.setObject(i+1,params[i]);
                }
            }
            rs = pstmt.executeQuery();
        }catch(Exception e){
            e.printStackTrace();
        }
        return rs;
    }

    /*关闭连接*/
    public static void close(ResultSet res, Statement sta, Connection conn) {
        try {
            if (res != null) {
                res.close();
            }
            if (sta != null) {
                sta.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void close(Statement sta, Connection conn) {
        close(null, sta, conn);
    }

    public static Long selectId() throws SQLException {
        int i = 0;
        Connection conn = JDBCUtils.getConnection();
        ResultSet rst = JDBCUtils.executeQuery(conn, "SELECT MAX(eid) FROM `employee`");
        while (rst.next()){
            i = rst.getByte(1);
        }
        JDBCUtils.close(rst,null,conn);
        return Long.valueOf(i+1);
    }

    /*随机生成六位密码*/
    public static String getPassword(){
        Random random = new Random();
        String result = "";
        for(int j = 0; j < 6; j++){
            result += random.nextInt(10);
        }
        return result;
    }

    /*生成时间戳*/
    public static Timestamp getDate(){
        Timestamp d = new Timestamp(System.currentTimeMillis());

        return d;
    }
    @Test
    void test(){
        System.out.println(getDate());
    }
}
